<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;

class BusinessController extends AdminbaseController{
    
	protected $businessModel;
	
	public function _initialize() {
		parent::_initialize();
		$this->businessModel = M("Business");
	}

    //商户列表
    public function index(){
        $where['is_checked']=1;
        $name=I('name');
        $company=I('company');
        $type=I('type',0,'intval');
        $scope_id=I('scope_id',0,'intval');
        $region_id=I('region_id',0,'intval');
        $is_prepared=I('is_prepared',99,'intval');
        if($name){
            $where['b.name']=$name;
        }
        if($company){
            $where['company']=$company;
        }
        if($type){
            $where['b.type']=$type;
        }
        if($scope_id){
            $where['scope_id']=$scope_id;
        }
        //查询地区
        if ($region_id) {
            $where['region_id'] = $region_id;
        }
        if($is_prepared!=99){
            $where['b.is_prepared']=$is_prepared;
        }
        $count=$this->businessModel
            ->alias('b')
            ->join("LEFT JOIN __SCOPE__ as s ON b.scope_id=s.id")
            ->where($where)
            ->count();
        $page = $this->page($count, 20);
        $businesses=$this->businessModel
            ->alias('b')
            ->join("LEFT JOIN __SCOPE__ as s ON b.scope_id=s.id")
            ->where($where)
            ->order("create_time DESC")
            ->field('b.*,s.name as sname')
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $scopes=M('Scope')->select();
        $provinces = lt_get_provinces();
        $this->assign('businesses',$businesses);
        $this->assign('scopes',$scopes);
        $this->assign('provinces',$provinces);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    //商户详情
    public function getDetails(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $business=$this->businessModel
            ->alias('b')
            ->join("LEFT JOIN __SCOPE__ as s ON b.scope_id=s.id")
            ->where(['is_checked'=>1,'b.id'=>$id])
            ->field('b.*,s.name as sname')
            ->find();
        if(!$business){
            $this->error('商户不存在');
        }
        $businessSlides=M('BusinessSlide')->where(['business_id'=>$id])->select();
        $businessTech=M('BusinessTech')->where(['business_id'=>$id])->select();
        $this->assign('business',$business);
        $this->assign('businessSlides',$businessSlides);
        $this->assign('businessTech',$businessTech);
        $this->display();
    }

    //商户禁用
    public function ban(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $business=$this->businessModel->where(['id'=>$id])->find();
        if($business){
            $result=$this->businessModel->where(['id'=>$id])->save(['status'=>0]);
            if($result!==false){
                $this->success('禁用成功');
            }else{
                $this->error('禁用失败');
            }
        }else{
            $this->error('商户不存在');
        }
    }


    //商户启用
    public function cancelban(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('缺少参数');
        }
        $business=$this->businessModel->where(['id'=>$id])->find();
        if($business){
            $result=$this->businessModel->where(['id'=>$id])->save(['status'=>1]);
            if($result!==false){
                $this->success('启用成功');
            }else{
                $this->error('启用失败');
            }
        }else{
            $this->error('商户不存在');
        }
    }

	// 待审核商户列表
	public function checkBusinesses(){
		$count=$this->businessModel->where(['is_checked'=>0,'status'=>['in',[1,5]]])->count();
		$page = $this->page($count, 20);
		$businesses=$this->businessModel->where(['is_checked'=>0,'status'=>['in',[1,5]]])->order("create_time DESC")->limit($page->firstRow, $page->listRows)->select();
		$scopes=lt_get_scopes_group();
		$this->assign("businesses",$businesses);
		$this->assign("page", $page->show('Admin'));
		$this->assign("scopes",$scopes);
		$this->display('checkBusinesses');
	}

    // 待审核商户详情
	public function checkDetails(){
        $id=I('id',0,'intval');
		$business=$this->businessModel->where(['id'=>$id,'is_checked'=>0,'status'=>['in',[1,5]]])->find();
        if($business){
            $qualifies=M('BusinessQualify')->where(['business_id'=>$id])->select();
            $scopes=lt_get_scopes_group();
            $this->assign("business",$business);
            $this->assign("scopes",$scopes);
            $this->assign("qualifies",$qualifies);
            $this->display('checkDetails');
        }else{
            $this->error('无此商户或已完成审核');
        }
	}

    /**
     * 待审核商户审核
     */
    public function checkCancelban(){
        $id=I('id',0,'intval');
        $business=M('Business')->where(['id'=>$id,'is_checked'=>0])->find();
        if($business){
            M('Business')->startTrans();
            try {
                $result=M('Business')->where(['id'=>$id])->save(['is_checked'=>1]);
                if ($result === false) {
                    throw new Exception('更改状态失败！');
                }
                //发短信
                $result=lt_send_sms($business['phone'],'【一号点评】您的商户审核已通过');
                if ($result === false) {
                    throw new Exception('短信发送失败！');
                }
                M('Business')->commit();
                $this->success('审核成功',U('Business/checkBusinesses'));
            } catch (Exception $e) {
                M('Business')->rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->error('商户不存在或已审核');
        }
    }

    /**
     * 待审核商户驳回
     */
    public function checkBan(){
		$id=I('id',0,'intval');
        $message=I('message');
        if(!$message){
            $this->error('请输入内容');
        }
		$business=M('Business')->where(['id'=>$id,'is_checked'=>0])->find();
        if($business){
            M('Business')->startTrans();
            try {
                $result=M('Business')->where(['id'=>$id])->save(['status'=>4]);
                if ($result === false) {
                    throw new Exception('更改状态失败！');
                }
                //发短信
                $result=lt_send_sms($business['phone'],'【一号点评】'.$message);
                if ($result === false) {
                    throw new Exception('短信发送失败！');
                }
                //删除上传的资质文件
                $result = M('BusinessQualify')->where(['business_id' => $id])->delete();
                if ($result === false) {
                    throw new Exception('删除资质上传失败！');
                }
                M('Business')->commit();
                $this->success('驳回成功');
            } catch (Exception $e) {
                M('Business')->rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->error('商户不存在或已驳回');
        }
    }
	
	//设置参数
	public function setParam(){
		if(IS_POST){
            $activityDay=I('activityDay',0,'intval');
            $result=sp_set_dynamic_config(['ACTIVITY_DAY'=>$activityDay]);
            if($result){
                $this->success('设置成功');
            }else{
                $this->success('设置失败');
            }
		}else{
            $activityDay=C('ACTIVITY_DAY');
            $this->assign('activityDay',$activityDay);
            $this->display('setParam');
		}
	}

    //改变资质
	public function changeQualify(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $business=$this->businessModel->where(['id'=>$id])->find();
        if($business){
            $type=($business['qualify_type']==1)?2:1;
            $result=$this->businessModel->where(['id'=>$id])->save(['qualify_type'=>$type]);
            if($result!==false){
                $this->success('改变成功');
            }else{
                $this->error('改变失败');
            }
        }else{
            $this->error('改变失败');
        }
    }

    //获取商户提现申请列表
    public function getBalances(){
        $business_name=I('business_name');
        $name=I('name');
        $is_solved=I('is_solved',99,'intval');
        if($business_name){
            $where['b.name']=$business_name;
        }
        if($name){
            $where['bb.name']=$name;
        }
        if($is_solved!=99){
            $where['is_solved']=$is_solved;
        }
        $count=M('BusinessBalance')->alias('bb')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=bb.business_id')
            ->field('bb.*,b.name as business_name,b.type as business_type')
            ->where($where)
            ->count();
        $page = $this->page($count, 20);
        $balances=M('BusinessBalance')->alias('bb')
            ->join('LEFT JOIN __BUSINESS__ as b ON b.id=bb.business_id')
            ->field('bb.*,b.name as business_name,b.type as business_type')
            ->where($where)
            ->order('bb.create_time DESC')
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign('balances',$balances);
        $this->assign("page", $page->show('Admin'));
        $this->display('getBalances');
    }

    //提现确认打款
    public function balanceSolve(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $balance=M('BusinessBalance')->where(['id'=>$id])->find();
        if($balance){
            $result=M('BusinessBalance')->where(['id'=>$id])->save(['is_solved'=>1]);
            if($result!==false){
                $this->success('改变成功');
            }else{
                $this->error('改变失败');
            }
        }else{
            $this->error('数据错误');
        }
    }

    //提现记录删除
    public function balanceDelete(){
        $id=I('id',0,'intval');
        if(!$id){
            $this->error('数据错误');
        }
        $balance=M('BusinessBalance')->where(['id'=>$id])->find();
        if($balance){
            $result=M('BusinessBalance')->where(['id'=>$id])->delete();
            if($result!==false){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('数据错误');
        }
    }

    //修改密码等
    public function resetPassword(){
        $id=I('id',0,'intval');
        $password=I('password');
        $url=$_POST['web_url'];
        if(!$id){
            $this->error('数据错误');
        }
        if(!$password&&!$url){
            $this->error('密码或url至少需要一个');
        }
        if($password){
            $data['prepare_password']=sp_password($password);
        }
        if($url){
            $data['url']=$url;
        }
        $business=M('Business')->where(['id'=>$id])->find();
        if($business){
            $result=M('Business')->where(['id'=>$id])->save($data);
            if($result!==false){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $this->error('商户不存在');
        }
    }
}